package field 
{
	import fl.motion.ColorMatrix;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author 
	 */
	public class Element extends MovieClip
	{
		static public const FALL_VELOCITY = 10;
		public var nextPosition: Point = null;
		
		protected var panel: GamePanel = null;
		protected var main: Main = null;
		public var tileX: Number = 0;
		public var tileY: Number = 0;
		
		
		public var isFalling: Boolean = false;
		public var isDisplacing: Boolean = false;
		
		public function Element() 
		{
			filters = [];
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			enableMouseControl();
		}
		
		public function onAddedToStage(e: Event) {
			panel = GamePanel(parent);
			main = Main(root);
			//registerInPanel();
			
			appear();
			
			tileX = panel.getElementTileX(this.x);
			tileY = panel.getElementTileY(this.y);
			
			nextPosition = new Point(this.x, this.y);
			
			buttonMode = false;
		};
		
		/*Appears current ball and return true if it is already appeared*/
		public function appear(): Boolean {
			var result: Boolean = (this.currentLabel == "Normal");
			
			if ( !result )
				if(currentLabel != "nextLevel")
					gotoAndPlay("Appearing");
				
			return result;
		}
		
		/*Make current balls blinking and 
		 * returns true if ball was already blinking*/
		public function blink(): Boolean {
			var result: Boolean = (this.currentLabel == "Blinking");
			
			if ( !result ) {
				gotoAndPlay("Blinking");
			}
			
			buttonMode = true;
			return result;
		}
		
		public function stopBlink(): void {
			buttonMode = false;
			gotoAndStop("Normal");
		}

		public function disappear(): void {
			gotoAndPlay("Disappearing");
		}
		
		public function isDisappearing(): Boolean {
			return currentLabel == "Disappearing";
		}
		public function onMouseOut(e: MouseEvent): void {
			panel.stopBlink();
		}	
		public function onMouseClick(e: MouseEvent): void {
			if (main.currentBonus != null)
				main.currentBonus.apply();
			else
				panel.disappearBallsUnderCursor();
			//disableMouseControl();
		}

				
		public function onMouseOver(e: MouseEvent): void {
			trace("Override me");
		}
		public function onEnterFrame(e: Event): void {
			if (isFalling) {
				//this.y = nextPosition.y - this.y >=  FALL_VELOCITY? this.y + FALL_VELOCITY: nextPosition.y; 
				this.y += Math.min( nextPosition.y - this.y, FALL_VELOCITY);
				if ( Math.round(nextPosition.y) == Math.round(this.y) ) {
					isFalling = false;
					panel.elementFinishedFall();
				}
			} else if (isDisplacing ) {
				this.x += Math.min( nextPosition.x - this.x, FALL_VELOCITY);
				if ( Math.round(nextPosition.x) == Math.round(this.x) ) {
					isDisplacing = false;
					panel.elementFinishedDisplace();
				}
			}
		}
		
		public function enableMouseControl(): void {
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);			
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);			
			addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		public function disableMouseControl(): void {
			removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);			
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			removeEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		public function onRemovedFromStage(e: Event): void {
			disableMouseControl();
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}		
	}

}